package com.hb.trade.api.routine.config;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.ArrayUtil;
import com.hb.trade.api.routine.config.web.TradeInterceptor;
import lombok.Getter;
import lombok.Setter;
import lombok.extern.slf4j.Slf4j;
import org.fzzn.core.model.constant.CommonConstant;
import org.fzzn.core.web.config.MessageConvertMvcConfigurer;
import org.springframework.context.annotation.Configuration;
import org.springframework.lang.NonNull;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;

import java.util.List;

/**
 * spring转换器声明(原 WebConfig)
 *
 * @author zhaojk
 */
@Slf4j
@Setter
@Getter
@Configuration
public class ApiMessageConvertWebMvcConfigurer extends MessageConvertMvcConfigurer {

    private final TradeInterceptor interceptor;

    public ApiMessageConvertWebMvcConfigurer(TradeInterceptor interceptor) {
        this.interceptor = interceptor;
    }

    /**
     * 拦截器相关处理
     *
     * @param registry
     */
    @Override
    public void addInterceptors(@NonNull InterceptorRegistry registry) {
        List<String> excludePathPatterns = CommonConstant.EXCLUDE_PATH_PATTERNS;
        if (CollUtil.isNotEmpty(excludePathPatterns)) {
            String[] excludes = ArrayUtil.toArray(excludePathPatterns, String.class);
            // 添加拦截器，配置拦截地址
            registry.addInterceptor(interceptor)
                    // 不拦截的请求
                    .excludePathPatterns(excludes);
        }
    }

    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("/v3/audiofile/**")
                .addResourceLocations("file:hall_audio/");
        super.addResourceHandlers(registry);
    }
}
